package com.sd.repay.task;

import com.sd.repay.common.Constants;
import com.sd.repay.pojo.WithdrawOrder;
import com.sd.repay.service.WithdrawNotifyBaseService;
import com.sd.repay.service.WithdrawOrderService;
import com.sd.repay.utils.WebUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 根据统一出账表状态通知业务处理
 */
@Component
public class NotifyWithdrawOrderResultTask {

    private static Logger log = LoggerFactory.getLogger(NotifyWithdrawOrderResultTask.class);

    @Autowired
    private WithdrawOrderService withdrawOrderService;

    @Autowired
    @Qualifier("repayWithdrawServiceImpl")
    private WithdrawNotifyBaseService repayWithdrawService;

    @Autowired
    @Qualifier("extractWithdrawServiceImpl")
    private WithdrawNotifyBaseService extractWithdrawService;


    public void executeWithdrawOrderNotify() {
        log.info("--------查询需要通知的统一出账记录---------");
        try {
            List<WithdrawOrder> unNotifyOrders = withdrawOrderService.getNeedNotifyWithdrawOrder();

            String withdrawService;
            String withdrawOrderNo;

            String notifyResult = "";
            log.info("--------需要通知的统一出账记录条数-------------" + unNotifyOrders.size());

            for (WithdrawOrder order : unNotifyOrders) {

                withdrawService = order.getService();
                withdrawOrderNo = order.getOrderNo();
                //业务通知
                try {
                    //还款
                    if (Constants.WITHDRAW_SERVICE_REPAY.equals(withdrawService)) {
                        notifyResult = repayWithdrawService.reciveWithdrawNotify(order);
                        //提现
                    } else if (Constants.WITHDRAW_SERVICE_WITHDRAW.equals(withdrawService)) {
                        notifyResult = extractWithdrawService.reciveWithdrawNotify(order);
                    }
                    log.info("统一出账订单号" + withdrawOrderNo + withdrawService + "业务通知结果,notifyResult=" + notifyResult);
                    if ("SUCCESS".equals(notifyResult)) {
                        int updateRow = withdrawOrderService.updateWithdrawOrderNotifyNum(withdrawOrderNo);
                        log.info("统一出账订单号" + withdrawOrderNo + "业务通知成功，通知次数加一影响的行数" + updateRow);

                        updateRow = withdrawOrderService.updateWithdrawOrderNotifyStatus(withdrawOrderNo, Constants.IS_NOT_NEED_NOTIFY);
                        log.info("统一出账订单号" + withdrawOrderNo + "业务通知成功，改为不需要通知状态影响的行数" + updateRow);
                    }

                } catch (Exception e) {
                    log.info("统一出账订单号" + withdrawOrderNo + withdrawService + "业务通知异常" + e);
                    e.printStackTrace();
                    continue;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("-----------统一出账订单业务分发通知任务完成--------------");
        WebUtil.removeBlockTaskBySpringId(WebUtil.getClassSimpleName(this.getClass()));
    }
}
